package com.mxc.scratch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lww.springcloudapi.entity.*;
import com.lww.springcloudapi.pojo.TabHallUsermessage;
import com.lww.springcloudapi.vo.CardObtain;
import com.lww.springcloudapi.vo.InfoInput;
import com.lww.springcloudapi.vo.InfoLog;
import com.mxc.scratch.mapper.*;
import com.mxc.scratch.service.TabGgcardInfoService;
import org.eclipse.jetty.util.ajax.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 刮刮卡信息表 服务实现类
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Service
public class TabGgcardInfoServiceImpl extends ServiceImpl<TabGgcardInfoMapper, TabGgcardInfo> implements TabGgcardInfoService {

    @Autowired
    TabGgcardInfoMapper tabGgcardInfoMapper;

    @Autowired
    TabGgcardStockMapper tabGgcardStockMapper;

    @Autowired
    TabGgcardLogMapper tabGgcardLogMapper;

    @Autowired
    TabGgcardObtainMapper tabGgcardObtainMapper;

    @Autowired
    TabHallUsermessageMapper tabHallUsermessageMapper;


    /**
     * 刮奖卡管理模块录入
     *
     * @param
     * @return
     */
    @Transactional
    public boolean inserInfo(Integer prizelevel,Integer stocknum , TabEmployee tabEmployee) {
        //查询该刮刮卡存入的归属点是否存在
        QueryWrapper<TabGgcardStock> tabGgcardStockQueryWrapper = new QueryWrapper<>();
        tabGgcardStockQueryWrapper.eq("ACPTSITEID", tabEmployee.getAcptsiteid());
        TabGgcardStock tabGgcardStock1 = tabGgcardStockMapper.selectOne(tabGgcardStockQueryWrapper);
        if (tabGgcardStock1 == null) {
            //不存在则插入一条新的库存数据
            TabGgcardStock tabGgcardStock=new TabGgcardStock();
            tabGgcardStock
                    .setAreaid("430100")
                    .setStocknum(stocknum)
                    .setTotalnum(stocknum)
                    .setLastIn(stocknum);
            int insert = tabGgcardStockMapper.insert(tabGgcardStock);
            if (stocknum>0) {
                for (int i = 0; i < stocknum; i++) {
                    TabGgcardInfo tabGgcardInfo=new TabGgcardInfo();
                    tabGgcardInfo
                            .setPrizelevel(prizelevel)
                            .setState("00")
                            .setTransn(UUID.randomUUID().toString().replace("-","").toUpperCase())
                            .setStatedate(new Date())
                            .setOptrid(tabEmployee.getEname())
                            .setAcptsiteid(tabEmployee.getAcptsiteid())
                            .setOptrdate(new Date());
                    //循环插入录入的刮刮卡信息
                    int insert1 = tabGgcardInfoMapper.insert(tabGgcardInfo);
                    if (insert1 < 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
                        return false;
                    }
                }
            } else {
                //不等于手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        } else {
            //存在做增加
            UpdateWrapper<TabGgcardStock> tabGgcardStockUpdateWrapper = new UpdateWrapper<>();
            tabGgcardStockUpdateWrapper
                    .set("TOTALNUM", stocknum)
                    .set("STOCKNUM", stocknum + tabGgcardStock1.getStocknum())
                    .set("LAST_IN", stocknum)
                    .eq("ACPTSITEID", tabEmployee.getAcptsiteid());
            int update = tabGgcardStockMapper.update(null, tabGgcardStockUpdateWrapper);
            //判断tabGgcardInfo.size()里面的录入信息条数是否等于要插入的库存数量
            if (stocknum>0) {
                for (int i = 0; i < stocknum; i++) {
                    TabGgcardInfo tabGgcardInfo=new TabGgcardInfo();
                    tabGgcardInfo
                            .setPrizelevel(prizelevel)
                            .setState("00")
                            .setTransn(UUID.randomUUID().toString().replace("-","").toUpperCase())
                            .setStatedate(new Date())
                            .setOptrid(tabEmployee.getEname())
                            .setAcptsiteid(tabEmployee.getAcptsiteid())
                            .setOptrdate(new Date());
                    //循环插入录入的刮刮卡信息
                    int insert1 = tabGgcardInfoMapper.insert(tabGgcardInfo);
                    if(insert1==0){
                        return false;
                    }
                }
            } else {
                //不等于手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        }
        return true;
    }

    /**
     * 修改查询刮刮卡修改信息
     * @param cardno
     * @return
     */
    public TabGgcardInfo getInfo(String cardno){
        QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper=new QueryWrapper<>();
        tabGgcardInfoQueryWrapper.eq("CARDNO",cardno);
         return tabGgcardInfoMapper.selectOne(tabGgcardInfoQueryWrapper);
    }

    /**
     * 刮奖卡管理模块修改业务
     *
     * @param
     * @return
     */
    @Transactional
    public boolean updateInfo(TabGgcardInfo tabGgcardInfo,TabEmployee tabEmployee) {
        System.out.println(tabGgcardInfo);
        //查询根据卡序号查询刮刮卡旧归属点
        QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper = new QueryWrapper<>();
        tabGgcardInfoQueryWrapper.eq("CARDNO", tabGgcardInfo.getCardno());
        TabGgcardInfo tabGgcardInfo1 = tabGgcardInfoMapper.selectOne(tabGgcardInfoQueryWrapper);
        UpdateWrapper<TabGgcardInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("PRIZELEVEL", tabGgcardInfo.getPrizelevel())//奖品等级 y
                .set("STATE", tabGgcardInfo.getState())//卡状态 y
                .set("STATEDATE",new Date())//状态时间 y
                .set("OPTRID",tabEmployee.getEname())//操作员 y
                .set("ACPTSITEID",tabGgcardInfo.getAcptsiteid())//受理点
                .set("OPTRDATE", new Date())//操作时间
                .set("TRANSN", UUID.randomUUID().toString().replace("-","").toUpperCase())//流水号UUid y
                .eq("CARDNO", tabGgcardInfo.getCardno());
        int update = tabGgcardInfoMapper.update(null, updateWrapper);
        //查询刮刮卡库存表
        QueryWrapper<TabGgcardStock> tabGgcardStockQueryWrapper = new QueryWrapper<>();
        tabGgcardStockQueryWrapper.eq("ACPTSITEID", tabGgcardInfo1.getAcptsiteid());
        TabGgcardStock tabGgcardStock = tabGgcardStockMapper.selectOne(tabGgcardStockQueryWrapper);
        //修改完对刮刮卡异动表进行新增\
        TabGgcardLog log = new TabGgcardLog();
        log
                .setAreaid("430100")//地区编码
                .setOldDept(tabGgcardInfo1.getAcptsiteid())//旧归属点
                .setNewDept(tabGgcardInfo.getAcptsiteid())//新归属点
                .setOrdertype("1")//操作类型
                .setCount(1)//数量
                .setOptrid(tabEmployee.getEname())//操作员
                .setAcptsiteid(tabEmployee.getAcptsiteid())//受理点
                .setOptrdate(new Date())//操作时间
                .setTransn(UUID.randomUUID().toString().replace("-","").toUpperCase());//流水号
        int insert = tabGgcardLogMapper.insert(log);
        //旧归属点库存修改-1
        UpdateWrapper<TabGgcardStock> tabGgcardStockUpdateWrapper = new UpdateWrapper<>();
        tabGgcardStockUpdateWrapper.set("STOCKNUM",tabGgcardStock.getStocknum()-1).eq("ACPTSITEID",tabGgcardInfo1.getAcptsiteid());
        int update1 = tabGgcardStockMapper.update(null, tabGgcardStockUpdateWrapper);
        //新归属点库存修改+1
        int jiayi = tabGgcardStockMapper.jiayi(tabGgcardInfo.getAcptsiteid());
        if (update == 0 || insert == 0 || update1 == 0||jiayi==0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 刮奖卡管理模块删除业务
     *
     * @param
     * @return
     */
    @Transactional
    public boolean deleteInfo(String cardno) {
        //查询删除的的刮刮卡是否被领取,领取的话就不能删除
        QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper = new QueryWrapper<>();
        tabGgcardInfoQueryWrapper.eq("CARDNO", cardno);
        TabGgcardInfo tabGgcardInfo1 = tabGgcardInfoMapper.selectOne(tabGgcardInfoQueryWrapper);
        if (tabGgcardInfo1.getState().equals("00")) {
            //没有就删除刮刮卡信息表对应的信息
            QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper1 = new QueryWrapper<>();
            tabGgcardInfoQueryWrapper1.eq("CARDNO", cardno);
            int delete = tabGgcardInfoMapper.delete(tabGgcardInfoQueryWrapper1);
            //对应的库存数量减一
            QueryWrapper<TabGgcardStock> tabGgcardStockQueryWrapper = new QueryWrapper<>();
            tabGgcardStockQueryWrapper.eq("ACPTSITEID", tabGgcardInfo1.getAcptsiteid());
            TabGgcardStock tabGgcardStock = tabGgcardStockMapper.selectOne(tabGgcardStockQueryWrapper);
            UpdateWrapper<TabGgcardStock> tabGgcardStockUpdateWrapper = new UpdateWrapper<>();
            tabGgcardStockUpdateWrapper.set("STOCKNUM", tabGgcardStock.getStocknum() - 1).eq("ACPTSITEID", tabGgcardInfo1.getAcptsiteid());
            int update = tabGgcardStockMapper.update(null, tabGgcardStockUpdateWrapper);
            if (delete > 0 || update > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 刮奖卡管理模块领取业务查询
     *
     * @param
     * @return
     */
    public String listScrapingCard(String yhpnumber, String glypnumber) {
        //查询用户输入的手机号码存不存在
        QueryWrapper<TabHallUsermessage> tabHallUsermessageQueryWrapper = new QueryWrapper<>();
        tabHallUsermessageQueryWrapper.eq("pnumber", yhpnumber);
        TabHallUsermessage tabHallUsermessage1 = tabHallUsermessageMapper.selectOne(tabHallUsermessageQueryWrapper);
        if (tabHallUsermessage1 != null) {
            //查询操作员属于那个营业厅
            QueryWrapper<TabHallUsermessage> user = new QueryWrapper<>();
            user.eq("pnumber", glypnumber);
            TabHallUsermessage tabHallUsermessage = tabHallUsermessageMapper.selectOne(user);
            //查询该营业厅未领取的刮刮卡
            QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper = new QueryWrapper<>();
            tabGgcardInfoQueryWrapper.eq("ACPTSITEID", tabHallUsermessage.getBusinesshall()).eq("STATE", 00);
            List<TabGgcardInfo> tabGgcardInfos = tabGgcardInfoMapper.selectList(tabGgcardInfoQueryWrapper);
            return JSON.toString(tabGgcardInfos);
        }
        return "该用户手机号码不存在";
    }

    /**
     * 刮奖卡管理模块领取业务领取刮刮卡
     *
     * @param cardObtain
     * @return
     */
    @Override
    @Transactional
    public boolean getAScrapingCard(CardObtain cardObtain) {
        //根据卡序列号查询刮刮卡信息
        QueryWrapper<TabGgcardInfo> tabGgcardInfoQueryWrapper = new QueryWrapper<>();
        tabGgcardInfoQueryWrapper.eq("CARDNO", cardObtain.getCardno());
        TabGgcardInfo tabGgcardInfo = tabGgcardInfoMapper.selectOne(tabGgcardInfoQueryWrapper);
        //修改要领取的刮刮卡信息
        UpdateWrapper<TabGgcardInfo> tabGgcardInfoUpdateWrapper = new UpdateWrapper<>();
        tabGgcardInfoUpdateWrapper
                .set("STATE", 10)//卡状态（00：未兑；10：已兑）
                .set("OPTRID", cardObtain.getTabGgcardObtain().getOptrid())//操作员
                .set("OPTRDATE", new Date())//操作时间
                .set("STATEDATE", new Date())//状态时间
                .eq("CARDNO", cardObtain.getCardno());

        int update = tabGgcardInfoMapper.update(null, tabGgcardInfoUpdateWrapper);
        //修改完刮刮卡修改刮刮卡库存表
        //查询库存
        QueryWrapper<TabGgcardStock> tasok = new QueryWrapper<>();
        tasok.eq("ACPTSITEID", tabGgcardInfo.getAcptsiteid());
        TabGgcardStock tabGgcardStock = tabGgcardStockMapper.selectOne(tasok);
        UpdateWrapper<TabGgcardStock> tabGgcardStockUpdateWrapper = new UpdateWrapper<>();
        tabGgcardStockUpdateWrapper.set("OBTAINNUM", tabGgcardStock.getObtainnum() + 1);
        int update1 = tabGgcardStockMapper.update(null, tabGgcardStockUpdateWrapper);
        //修改完库存表添加刮刮卡领取日志表
        //查询用户编码
        TabGgcardObtain tabGgcardObtain = new TabGgcardObtain();
        tabGgcardObtain
                .setAreaid(tabGgcardStock.getAreaid())//地区编码
                .setSvcid(cardObtain.getTabGgcardObtain().getSvcid())//业务类型 1
                .setJoinmode(cardObtain.getTabGgcardObtain().getJoinmode())//参与方式 1
                .setObtainnum(1)//领取数量
                .setObtainstate("00")//领取状态
                .setJoinpara(cardObtain.getTabGgcardObtain().getJoinpara())//参与条件
                .setOptrid(cardObtain.getTabGgcardObtain().getOptrid())//操作员
                .setAcptsiteid(tabGgcardInfo.getAcptsiteid())//受理点
                .setOptrdate(new Date())//操作时间
                .setTransn(cardObtain.getTabGgcardObtain().getTransn())//流水号 1
                .setRemarks(cardObtain.getTabGgcardObtain().getRemarks());//备注 1
        int insert = tabGgcardObtainMapper.insert(tabGgcardObtain);
        if (update > 0 || update1 > 0 || insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 刮奖卡管理模块查询业务
     *
     * @param prizelevel 礼品等级
     * @param state  卡状态
     * @param pageNo
     * @param pageSize
     * @param acptsiteid 受理点
     * @return
     */
    public Page queryByAreaId(String acptsiteid,String prizelevel, String state, Integer pageNo, Integer pageSize) {
        Page<TabGgcardInfo> page = new Page(pageNo, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(prizelevel != null && prizelevel != "", "PRIZELEVEL", prizelevel);
        queryWrapper.eq(state != null && state != "", "STATE", state).eq("ACPTSITEID",acptsiteid);
        page = tabGgcardInfoMapper.selectPage(page, queryWrapper);
        return page;
    }


}
